package com.hub.transform;

import com.hub.entity.MySensor;
import com.hub.entity.SensorReading;
import org.apache.commons.compress.utils.Lists;
import org.apache.flink.streaming.api.collector.selector.OutputSelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Collections;

public class TransformSplitFromCustom {

    public static void main(String[] args) throws Exception {

        // 环境准备
        StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();

        // 添加数据源：自定义数据源
        DataStreamSource<SensorReading> dataStreamSource = executionEnvironment.addSource(new MySensor());


        DataStream<SensorReading> select = dataStreamSource.split(new OutputSelector<SensorReading>() {

            @Override
            public Iterable<String> select(SensorReading o) {
                if (o.getTemplate() > 5) {
                    return Collections.singleton("high template");
                }
                return Collections.singleton("low template");
            }
        }).select("high template");

        // 打印任务
        select.print("high_from_custom");

        // 执行
        executionEnvironment.execute();


    }
}
